package br.ucb.tcc.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import net.java.dev.designgridlayout.DesignGridLayout;
import br.ucb.tcc.negocio.TwitterBean;
import br.ucb.tcc.utils.Constantes;
import br.ucb.tcc.utils.FileUtils;
import br.ucb.tcc.utils.StringUtil;
import br.ucb.tcc.validation.CampoObrigatorioValidator;

public class InformacoesLoginDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField login;
	private JPasswordField senha;
	private JTextField consumer;
	private JPasswordField consumerSecret;
	private JTextField acessToken;
	private JPasswordField acessTokenSecret;
	private JButton logar;
	private JButton cancelar;

	public InformacoesLoginDialog() {
		setSize(400, 200);
		setModal(true);
		add(painelCentral());
		setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Integração com Twitter");
	}

	private JPanel painelCentral() {
		JPanel panelCentral = new JPanel();
		DesignGridLayout gridLayout = new DesignGridLayout(panelCentral);
		gridLayout.row().grid(new JLabel("Login")).add(getLogin());
		gridLayout.row().grid(new JLabel("Senha")).add(getSenha());
		gridLayout.row().grid(new JLabel("ConsumerKey")).add(getConsumer());
		gridLayout.row().grid(new JLabel("ConsumerSecret")).add(getConsumerSecret());
		gridLayout.row().center().add(getLogar()).add(getCancelar());
		return panelCentral;
	}

	/**
	 * @return the logar
	 */
	public JButton getLogar() {
		if (logar == null) {
			logar = new JButton();
			logar.setText("Autenticar");
			logar.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					System.out.println("#####INFORMACOES LOGIN#####\nLogin:" + getLogin().getText()
							+ "\nSenha:" + getSenha().getText() + "\nConsumer:" + getConsumer().getText()
							+ "\nConsumerSecret:" + getConsumerSecret().getText());
					if (StringUtil.isStringVazia(getLogin().getText())
							|| StringUtil.isStringVazia(getSenha().getText())
							|| StringUtil.isStringVazia(getConsumer().getText())
							|| StringUtil.isStringVazia(getConsumerSecret().getText())) {
						JOptionPane.showMessageDialog(null, "Campos obrigatórios não preenchidos");
					} else if (TwitterBean.autentica(getConsumer().getText(), getConsumerSecret().getText())) {
						try {
							HashMap<String, String> maps = new HashMap<String, String>(0);
							JOptionPane.showMessageDialog(null, "Bem vindo! @" + TwitterBean.obterNome()
									+ ".Autenticação realizada com sucesso!");
							maps.put("login", getLogin().getText());
							maps.put("senha", getSenha().getText());
							maps.put("consumer", getConsumer().getText());
							maps.put("consumerSecret", getConsumerSecret().getText());
							maps.put("logado", Constantes.LOGADO);
							FileUtils.criarArquivoProperties(Constantes.PATH, Constantes.ARQUIVO_LOGIN, maps);
							dispose();
						} catch (Exception e) {
							JTextArea teste = new JTextArea(10, 50);
							teste.setText(e.getMessage());
							javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane(teste);
							JOptionPane.showMessageDialog(null, scrollPane);
							limpar();
						}
					} else {
						JOptionPane.showMessageDialog(null, "Falha na autenticaçao");
						limpar();
					}

				}
			});
		}
		return logar;
	}

	public void limpar() {
		getLogin().setText("");
		getSenha().setText("");
		getConsumer().setText("");
		getConsumerSecret().setText("");
	}

	public JButton getCancelar() {
		if (cancelar == null) {
			cancelar = new JButton();
			cancelar.setText("Cancelar");
			cancelar.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					System.exit(0);

				}
			});
		}
		return cancelar;
	}

	/**
	 * @return the login
	 */
	public JTextField getLogin() {
		if (login == null) {
			login = new JTextField();
			login.setInputVerifier(new CampoObrigatorioValidator(login));
		}
		return login;
	}

	/**
	 * @return the senha
	 */
	public JPasswordField getSenha() {
		if (senha == null) {
			senha = new JPasswordField();
			senha.setInputVerifier(new CampoObrigatorioValidator(senha));
		}
		return senha;
	}

	/**
	 * @return the consumer
	 */
	public JTextField getConsumer() {
		if (consumer == null) {
			consumer = new JTextField();
			consumer.setInputVerifier(new CampoObrigatorioValidator(consumer));
		}
		return consumer;
	}

	/**
	 * @return the consumerSecret
	 */
	public JPasswordField getConsumerSecret() {
		if (consumerSecret == null) {
			consumerSecret = new JPasswordField();
			consumerSecret.setInputVerifier(new CampoObrigatorioValidator(consumerSecret));
		}
		return consumerSecret;
	}

	/**
	 * @return the acessToken
	 */
	public JTextField getAcessToken() {
		if (acessToken == null) {
			acessToken = new JTextField();
		}
		return acessToken;
	}

	/**
	 * @return the acessTokenSecret
	 */
	public JPasswordField getAcessTokenSecret() {
		if (acessTokenSecret == null) {
			acessTokenSecret = new JPasswordField();
		}
		return acessTokenSecret;
	}

}
